package com.huanbao.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.huanbao.base.dto.ExchangeDto;
import com.huanbao.base.lang.Result;
import com.huanbao.entity.ExchangeRecord;
import com.huanbao.entity.PersonalIntegralNumber;
import com.huanbao.entity.PersonalMessage;
import com.huanbao.entity.ShoppingMall;
import com.huanbao.mapper.ExchangeRecordMapper;
import com.huanbao.mapper.PersonalIntegralNumberMapper;
import com.huanbao.mapper.PersonalMessageMapper;
import com.huanbao.mapper.ShoppingMallMapper;
import com.huanbao.service.ShoppingMallService;
import com.huanbao.service.UserService;
import com.huanbao.util.ShiroUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.time.LocalDateTime;

/**
 * <p>
 * 积分商城所剩物品表 服务实现类
 * </p>
 *
 * @author zq
 * @since 2021-04-29
 */
@Service
public class ShoppingMallServiceImpl extends ServiceImpl<ShoppingMallMapper, ShoppingMall> implements ShoppingMallService {

    @Resource
    private ShoppingMallMapper shoppingMallMapper;

    @Resource
    private ExchangeRecordMapper exchangeRecordMapper;

    @Resource
    private PersonalMessageMapper messageMapper;

    @Resource
    private PersonalIntegralNumberMapper integralNumberMapper;

    @Autowired
    private UserService userService;

    @Override
    @Transactional
    public Result exchange(ExchangeDto exchange) {

        Long userId = ShiroUtil.getProfile().getId();
        Long itemId = exchange.getItemId();
        ShoppingMall shoppingMall = shoppingMallMapper.selectById(itemId);
        PersonalIntegralNumber user = integralNumberMapper.selectOne(new QueryWrapper<PersonalIntegralNumber>().eq("user_id", userId));
        //如果用户积分数小于所需消耗积分数
        if (exchange.getNumber() * shoppingMall.getIntegralNumber() > user.getIntegralNumber()) {
            return Result.fail("您的剩余积分不足，请查看。");
        }
        //更新用户积分数
        int number = exchange.getNumber() * shoppingMall.getIntegralNumber();
        userService.IntegralModify(userId, 1, number);

        //更新积分商城该商品数量
        shoppingMall.setNumber(shoppingMall.getNumber() - exchange.getNumber());
        shoppingMallMapper.updateById(shoppingMall);

        //消耗积分
        int creditsExchange = shoppingMall.getNumber() * exchange.getNumber();
        ExchangeRecord exchangeRecord = new ExchangeRecord();
        exchangeRecord.setUserId(userId)
                .setItemId(itemId)
                .setNumber(exchange.getNumber())
                .setItemName(shoppingMall.getItemName())
                .setIntegralNumber(creditsExchange)
                .setUserPhone(exchange.getUserPhone())
                .setUserAddress(exchange.getUserAddress());
        //插入兑换记录
        exchangeRecordMapper.insert(exchangeRecord);

        String message = "您已兑换商品" + shoppingMall.getItemName() + exchange.getNumber() + "个，后台正在加紧配送中，请耐心等待。";
        PersonalMessage personalMessage = new PersonalMessage();
        personalMessage.setUserId(userId)
                .setContent(LocalDateTime.now() + message);
        messageMapper.insert(personalMessage);

        return Result.success(200, message, null);
    }
}
